home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Sound / PreludeAMP / src / layer2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-06-16  |  8.4 KB  |  321 lines

  1. /* this file is a part of amp software, (C) tomislav uzelac 1996,1997
  2. */
  3.  
  4. /* layer2.c   MPEG audio layer2 support
  5.  *
  6.  * Created by: Tomislav Uzelac  Mar 1996
  7.  * merged with amp, May 19 1997
  8.  */
  9. #include "amp.h"
  10. #include "audio.h"
  11. #include "getbits.h"
  12. #include "transform.h"
  13.  
  14. #define LAYER2
  15. #include "layer2.h"
  16.  
  17. int layer2_frame(struct AUDIO_HEADER *header,int cnt)
  18. {
  19. int i,s,sb,ch,gr,bitrate,bound;
  20. char (*nbal)[],(*bit_alloc_index)[][16];
  21. unsigned char allocation[2][32];
  22. unsigned char scfsi[2][32];
  23. float scalefactor[2][32][3];
  24. float subband_sample[2][32][36];
  25. int sblimit,nlevels,grouping; 
  26.  
  27. float  c,d;
  28. int no_of_bits,mpi;                
  29. unsigned short sb_sample_buf[3];    
  30.  
  31. int hsize,fs,mean_frame_size;
  32.  
  33.     hsize=4;
  34.     if (header->protection_bit==0) hsize+=2;
  35.  
  36.     bitrate=t_bitrate[header->ID][3-header->layer][header->bitrate_index];
  37.         fs=t_sampling_frequency[header->ID][header->sampling_frequency];
  38.         if (header->ID) mean_frame_size=144000*bitrate/fs;
  39.         else mean_frame_size=72000*bitrate/fs;
  40.  
  41.     /* layers 1 and 2 do not have a 'bit reservoir'
  42.      */
  43.     append=data=0;
  44.  
  45.     fillbfr(mean_frame_size + header->padding_bit - hsize);
  46.  
  47.     switch (header->mode)
  48.         {
  49.         case 0 : 
  50.         case 2 : nch=2; bound=32; bitrate=bitrate/2;  
  51.             break;
  52.         case 3 : nch=1; bound=32; 
  53.             break;
  54.         case 1 : nch=2; bitrate=bitrate/2; bound=(header->mode_extension+1)*4; 
  55.         }
  56.         
  57.     if (header->ID==1) switch (header->sampling_frequency) {
  58.         case 0 : switch (bitrate)    /* 0 = 44.1 kHz */
  59.                 {
  60.                 case 56  :
  61.                 case 64  :
  62.                 case 80  : bit_alloc_index=&t_alloc0;
  63.                        nbal=&t_nbal0;
  64.                        sblimit=27;
  65.                        break;
  66.                 case 96  :
  67.                 case 112 :
  68.                 case 128 :
  69.                 case 160 :
  70.                 case 192 : bit_alloc_index=&t_alloc1;
  71.                        nbal=&t_nbal1;
  72.                        sblimit=30;  
  73.                        break;
  74.                 case 32  :
  75.                 case 48  : bit_alloc_index=&t_alloc2;
  76.                        nbal=&t_nbal2;
  77.                        sblimit=8;
  78.                        break;
  79.                 default  : printf(" bit alloc info no gud ");
  80.                 }
  81.                 break;
  82.         case 1 : switch (bitrate)    /* 1 = 48 kHz */
  83.                 {
  84.                 case 56  : 
  85.                 case 64  :
  86.                 case 80  :
  87.                 case 96  :
  88.                 case 112 :
  89.                 case 128 :
  90.                 case 160 :
  91.                 case 192 : bit_alloc_index=&t_alloc0;
  92.                        nbal=&t_nbal0;
  93.                        sblimit=27;
  94.                        break;
  95.                 case 32  :
  96.                 case 48  : bit_alloc_index=&t_alloc2;
  97.                        nbal=&t_nbal2;
  98.                        sblimit=8;
  99.                        break;
  100.                 default  : printf(" bit alloc info no gud ");
  101.                 }
  102.                 break;
  103.         case 2 : switch (bitrate)    /* 2 = 32 kHz */
  104.                 {
  105.             case 56  :
  106.                         case 64  :
  107.                         case 80  : bit_alloc_index=&t_alloc0;
  108.                                    nbal=&t_nbal0;
  109.                                    sblimit=27;
  110.                                    break;
  111.                         case 96  :
  112.                         case 112 :
  113.                         case 128 :
  114.                         case 160 :
  115.                         case 192 : bit_alloc_index=&t_alloc1;
  116.                                    nbal=&t_nbal1;
  117.                                    sblimit=30;
  118.                                    break;
  119.             case 32  :
  120.             case 48  : bit_alloc_index=&t_alloc3;
  121.                                    nbal=&t_nbal3;
  122.                                    sblimit=12;
  123.                    break;
  124.             default  : printf("bit alloc info not ok\n");
  125.             }
  126.                     break;                                                    
  127.         default  : printf("sampling freq. not ok/n");
  128.     } else {
  129.         bit_alloc_index=&t_allocMPG2;
  130.         nbal=&t_nbalMPG2;
  131.         sblimit=30;
  132.     }
  133.  
  134. /*
  135.  * bit allocation per subband per channel decoding *****************************
  136.  */
  137.  
  138.     if (bound==32) bound=sblimit;    /* bound=32 means there is no intensity stereo */
  139.      
  140.     for (sb=0;sb<bound;sb++)
  141.         for (ch=0;ch<nch;ch++)
  142.             allocation[ch][sb]=getbits((*nbal)[sb]);
  143.  
  144.     for (sb=bound;sb<sblimit;sb++)
  145.         allocation[1][sb] = allocation[0][sb] = getbits((*nbal)[sb]);
  146.  
  147.  
  148. /*
  149.  * scfsi ***********************************************************************
  150.  */
  151.  
  152.     for (sb=0;sb<sblimit;sb++)
  153.         for (ch=0;ch<nch;ch++)
  154.             if (allocation[ch][sb]!=0) scfsi[ch][sb]=getbits(2);
  155.             else scfsi[ch][sb]=0;
  156.  
  157. /*
  158.  * scalefactors ****************************************************************
  159.  */
  160.  
  161.     for (sb=0;sb<sblimit;sb++)
  162.     for (ch=0;ch<nch;ch++)
  163.         if (allocation[ch][sb]!=0) {
  164.             scalefactor[ch][sb][0]=t_scalefactor[getbits(6)];
  165.             switch (scfsi[ch][sb])
  166.             {
  167.             case 0: scalefactor[ch][sb][1]=t_scalefactor[getbits(6)];
  168.                 scalefactor[ch][sb][2]=t_scalefactor[getbits(6)];
  169.                 break;
  170.             case 1: scalefactor[ch][sb][2]=t_scalefactor[getbits(6)];
  171.                 scalefactor[ch][sb][1]=scalefactor[ch][sb][0];
  172.                 break;
  173.             case 2: scalefactor[ch][sb][1]=scalefactor[ch][sb][0];
  174.                 scalefactor[ch][sb][2]=scalefactor[ch][sb][0];
  175.                 break;
  176.             case 3: scalefactor[ch][sb][2]=t_scalefactor[getbits(6)];
  177.                 scalefactor[ch][sb][1]=scalefactor[ch][sb][2];
  178.             }             
  179.         } 
  180.         else scalefactor[ch][sb][0]=scalefactor[ch][sb][1]=\
  181.             scalefactor[ch][sb][2]=0.0;
  182.  
  183.  
  184. /*
  185.  * samples *********************************************************************
  186.  */
  187.  
  188.     for (gr=0;gr<12;gr++) {
  189.         /*
  190.          * normal ********************************
  191.          */
  192.        
  193.         for (sb=0;sb<bound;sb++)
  194.         for (ch=0;ch<nch;ch++)
  195.         if (allocation[ch][sb]!=0) {
  196.             mpi=(*bit_alloc_index)[sb][allocation[ch][sb]];    
  197.             no_of_bits=t_bpc[mpi];
  198.             c=t_c[mpi];
  199.             d=t_d[mpi];
  200.             grouping=t_grouping[mpi];
  201.             nlevels=t_nlevels[mpi];
  202.  
  203.             if (grouping) {
  204.                 int samplecode=getbits(no_of_bits);
  205.                 convert_samplecode(samplecode,grouping,sb_sample_buf);
  206.  
  207.                             for (s=0;s<3;s++)
  208.                                     subband_sample[ch][sb][3*gr+s]=requantize_sample (sb_sample_buf[s],nlevels,c,d,scalefactor[ch][sb][gr/4]);
  209.             } else {
  210.                 for (s=0;s<3;s++) sb_sample_buf[s]=getbits(no_of_bits);
  211.                 
  212.                 for (s=0;s<3;s++) { 
  213.                     /*subband_sample[ch][sb][3*gr+s]=requantize_sample (sb_sample_buf[s],nlevels,c,d,scalefactor[ch][sb][gr/4]);*/
  214.                     subband_sample[ch][sb][3*gr+s]=(t_dd[mpi]+sb_sample_buf[s]*t_nli[mpi])*c*scalefactor[ch][sb][gr>>2];
  215.                 }
  216.             }
  217.             } else 
  218.             for (s=0;s<3;s++) subband_sample[ch][sb][3*gr+s]=0;
  219.  
  220.  
  221.         /*
  222.          * joint stereo ********************************************
  223.          */
  224.       
  225.         for (sb=bound;sb<sblimit;sb++)
  226.            if (allocation[0][sb]!=0) {
  227.             /*ispravka!
  228.             */
  229.             mpi=(*bit_alloc_index)[sb][allocation[0][sb]];    
  230.             no_of_bits=t_bpc[mpi];
  231.             c=t_c[mpi];
  232.             d=t_d[mpi];
  233.             grouping=t_grouping[mpi];
  234.             nlevels=t_nlevels[mpi];    
  235.                
  236.             if (grouping) {
  237.                 int samplecode=getbits(no_of_bits);
  238.                 convert_samplecode(samplecode,grouping,sb_sample_buf);
  239.  
  240.                 for (s=0;s<3;s++) {
  241.                     subband_sample[0][sb][3*gr+s]=requantize_sample (sb_sample_buf[s],nlevels,c,d,scalefactor[0][sb][gr/4]);
  242.                     subband_sample[1][sb][3*gr+s]=subband_sample[0][sb][3*gr+s];
  243.                 }
  244.             } else {
  245.                 for (s=0;s<3;s++) sb_sample_buf[s]=getbits(no_of_bits);
  246.  
  247.                 for (s=0;s<3;s++) { 
  248.                     subband_sample[0][sb][3*gr+s]=subband_sample[1][sb][3*gr+s]=\
  249.                         (t_dd[mpi]+sb_sample_buf[s]*t_nli[mpi])*c*scalefactor[0][sb][gr>>2];
  250.                 }
  251.             }
  252.  
  253.         } else for (s=0;s<3;s++) {
  254.             subband_sample[0][sb][3*gr+s]=0;
  255.             subband_sample[1][sb][3*gr+s]=0;
  256.         }
  257.  
  258.         /*
  259.          * the rest *******************************************
  260.          */
  261.         for (sb=sblimit;sb<32;sb++)
  262.             for (ch=0;ch<nch;ch++)
  263.  
  264.                 for (s=0;s<3;s++) subband_sample[ch][sb][3*gr+s]=0;
  265.     }    
  266.  
  267.     /*
  268.      * this is, in fact, horrible, but I had to adjust it to amp/mp3. The hack to make downmixing
  269.      * work is as ugly as possible.
  270.      */
  271.  
  272.     if (A_DOWNMIX && header->mode!=3) {
  273.         for (ch=0;ch<nch;ch++) 
  274.             for (sb=0;sb<32;sb++)
  275.                 for (i=0;i<36;i++)
  276.                     subband_sample[0][sb][i]=(subband_sample[0][sb][i]+subband_sample[1][sb][i])*0.5f;
  277.         nch=1;
  278.     }
  279.  
  280.     for (ch=0;ch<nch;ch++) {
  281.         for (sb=0;sb<32;sb++) 
  282.             for (i=0;i<18;i++) res[sb][i]=subband_sample[ch][sb][i]; 
  283.            for (i=0;i<18;i++)
  284.             poly(ch,i);
  285.     }
  286.     printout();
  287.     for (ch=0;ch<nch;ch++) {
  288.                 for (sb=0;sb<32;sb++)
  289.                         for (i=0;i<18;i++) res[sb][i]=subband_sample[ch][sb][i+18];
  290.                 for (i=0;i<18;i++)
  291.                         poly(ch,i);
  292.         }
  293.         printout();
  294.  
  295.     if (A_DOWNMIX && header->mode!=3) nch=2;
  296.  
  297.     return 0;
  298. }                        
  299. /****************************************************************************/    
  300. /****************************************************************************/
  301.  
  302. void convert_samplecode(unsigned int samplecode,unsigned int nlevels,unsigned short* sb_sample_buf)
  303. {
  304. int i;
  305.  
  306.     for (i=0;i<3;i++) {
  307.         *sb_sample_buf=samplecode%nlevels;
  308.         samplecode=samplecode/nlevels;
  309.         sb_sample_buf++;
  310.     }
  311. }
  312.  
  313. float requantize_sample(unsigned short s4,unsigned short nlevels,float c,float d,float factor)
  314. {
  315. register float s,s2,s3;
  316. s3=-1.0+s4*2.0/(nlevels+1);
  317. s2=c*(s3+d);
  318. s=factor*s2;
  319. return s;
  320.